﻿/*   
    Copyright (C) 2009 Galaktika Corporation ZAO

    This file is a part of Galaktika.BusinessMonitor
 
    Galaktika.BusinessMonitor is a free software: you can redistribute it and/or modify
    it under the terms of the GNU General Public License as published by
    the Free Software Foundation, either version 3 of the License, or
    (at your option) any later version.
      
    You should have received a copy of the GNU General Public License
    along with Galaktika.BusinessMonitor.  If not, see <http://www.gnu.org/licenses/>.
  
    If GPL v.3 is not suitable for your products or company,
    Galaktika Corp provides Galaktika.BusinessMonitor under a flexible commercial license
    designed to meet your specific usage and distribution requirements.
    If you have already obtained a commercial license from Galaktika Corp,
    you can use this file under those license terms.
*/

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;

namespace Galaktika.BI.Silverlight.Controls
{
    using Galaktika.BI.Runtime;
    using Galaktika.BI.Runtime.Services;
    using Galaktika.BI.Extensibility.Dom;
    using Galaktika.BI.Silverlight.Intermediate;
    using Galaktika.BI.Silverlight.Runtime.Data;

    public class SimpleDataGrid : DataGrid, IBindableObject, IEditableObject
    {
        public SimpleDataGrid()
        {
        }

        public static DependencyProperty ADataSourceProperty = DependencyProperty.Register("ADataSource", typeof(object), typeof(SimpleDataGrid), null);
        public object ADataSource 
        {
            get
            {
                return base.GetValue(ADataSourceProperty);
            }
            set
            {
                base.SetValue(ADataSourceProperty, value);
            }
        }

        /// <summary>
        /// Задает действие, выполняющееся при двойном щелчке и нажатии клавиши Enter
        /// на выделенной строке
        /// </summary>
        public object ARowCustomAction { get; set; }

        /// <summary>
        /// Строка, представляющая фильтр отображаемых столбцов из свойства ARowCustomAction
        /// </summary>
        public string ADisplayableColumns { get; set; }


        #region IBindableObject Members

        public IBindingContext Context { get; set; }

        private IDictionary<string, IBindingManager> m_Bindings;
        public IDictionary<string, IBindingManager> Bindings
        {
            get
            {
                if (m_Bindings == null)
                {
                    m_Bindings = new SafeDictionary<string, IBindingManager>();
                }

                return m_Bindings;
            }
        }

        #endregion

        #region IEditableObject Members

        void IEditableObject.BeginEdit()
        {
        }

        void IEditableObject.CancelEdit()
        {
        }

        void IEditableObject.EndEdit()
        {
            DataTableWrapper data = this.ADataSource as DataTableWrapper;
            if (data != null)
            {
                this.Columns.Clear();
                this.ItemsSource = null;
                var source = DataTable.Create(data);
                this.AutoGenerateColumns = false;
                int i = 0;
                foreach (var entry in source.Columns)
                {
                    this.Columns.Add(new DataGridTextColumn() { Header = entry.Name, Binding = new Binding(String.Format("[{0}].Value",i)) });
                    i++;
                }                
                this.ItemsSource = source.Rows;
                var m_Manager = m_Bindings["ADataSource"];
                if (m_Manager != null)
                {
                    this.SelectedIndex = m_Manager.Position[0];
                }
                else
                    this.SelectedIndex = 0;
            }
            
        }

        #endregion
    }
}
